package com.jz.community.basecomm.task;

import android.app.Activity;

import com.jz.community.basecomm.bean.baseCoupon.BaseCouponInfo;
import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.net.OkHttpUtil;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.JsonUtils;
import com.jz.community.basecomm.utils.Preconditions;

/**
 * Created by Tony.Fan on 2018/5/29 15:16
 * 通用领券
 */
public class GetReceiveCouponTask extends RxTask<String, Integer, BaseCouponInfo> {

    private Activity activity;
    private ITaskCallbackListener taskListener;

    public GetReceiveCouponTask(Activity activity,
                                ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }

    @Override
    protected BaseCouponInfo doInBackground(String... params) {
        // 执行请求
        String result = OkHttpUtil.post(Constant.RECEIVE_COUPON_URL, getParam(params[0]));
        BaseCouponInfo info = null;
        if (!Preconditions.isNullOrEmpty(result)) {
            info = JsonUtils.parseObject(result, BaseCouponInfo.class);
        }
        return info;
    }

    @Override
    protected void onPostExecute(BaseCouponInfo result) {
        // 执行结果回调函数
        taskListener.doTaskComplete(result);
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    /**
     * 获取请求所需的参数
     *
     * @param
     * @return
     */
    private String getParam(String couponId) {
        StringBuffer sb = new StringBuffer();
        sb.append("couponId=" + ConverterUtils.toLong(couponId));
        return sb.toString();
    }
}